//
//  LGTTaskConditionAlertVC.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/9.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit

@objc protocol LGTTaskConditionAlertDelegate{
    func dismiss(_ vc:LGTTaskConditionAlertVC)
    @objc optional
    func selectIndex(_ index:Int, _ Row:Int)
}
    
class LGTTaskConditionAlertVC: LGTBaseVC, UITableViewDelegate, UITableViewDataSource{
    var index:Int = 0
    var tableView:UITableView?
    var conditionList:ConditionListModel?
    weak var delegate:LGTTaskConditionAlertDelegate?
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func setUI() {
        super.setUI()
        self.view.backgroundColor = UIColor.clear
        let mainView:UIView = UIView.init()
        mainView.backgroundColor = BlackAlphaColor
        self.view.addSubview(mainView)
        mainView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalTo(0)
            make.top.equalTo(NavBarH())
        }
        
        self.tableView = UITableView.init(frame:  CGRect.init(x: 0.0, y: 0, width: KScreenW, height: CGFloat(SizeFit(120))), style: UITableView.Style.plain)
        self.tableView?.separatorStyle = UITableViewCell.SeparatorStyle.none
        self.tableView?.delegate = self
        self.tableView?.dataSource = self
        self.tableView?.estimatedRowHeight = 50
        self.tableView?.rowHeight = UITableView.automaticDimension
        self.tableView?.register(LGTTaskConditionCell.classForCoder(), forCellReuseIdentifier: "cell")
        mainView.addSubview(self.tableView!)
    }
    
//    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//        self.dismiss(animated: false, completion: nil)
//    }
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.conditionList?.list?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:LGTTaskConditionCell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! LGTTaskConditionCell
      //  cell.model = self.conditionList?.list?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if self.delegate?.selectIndex?(self.index, indexPath.row) != nil{
            
        }
        self.dismissVC()
    }
    
    func scrollToTop(){
        self.tableView?.scrollsToTop = true
        self.tableView?.reloadData()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismissVC()
    }
    
    func dismissVC(){
        self.delegate?.dismiss(self)
        self.dismiss(animated: false, completion: nil)
    }
}
